import Vue from 'vue'
import Vuex from 'vuex'
import { login, logout, phoneNoLogin,oaPhoneLogin } from "@/api"
import { ACCESS_TOKEN,USER_NAME,USER_INFO } from "@/util/constants"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    userid:'',
    username: '',
    realname: '',
    welcome: '',
    avatar: '',
    mainPageActive:'',
	mqClient:'',
    phone:''
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { username, realname, welcome }) => {
      state.username = username
      state.realname = realname
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_PAGE_ACTIVE:(state,active)=>{
      state.mainPageActive = active
    },
    SET_MQCLIENT:(state,mqClient)=>{
      state.mqClient = mqClient
    }
  },
  actions: {
    // 登录
    mLogin({ commit }, userInfo) {
      console.log("mLogin",userInfo)
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          console.log("response",response)
          if(response.code =='200'){
            const result = response.result
            const userInfo = result.userInfo
            Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_NAME, userInfo.username, 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
            commit('SET_TOKEN', result.token)
            commit('SET_AVATAR', userInfo.avatar)
            commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname})
            resolve(response)
          }else{
            reject(response)
          }

        }).catch(error => {
          reject(error)
        })
      })
    },
    //手机号登录
    PhoneLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        phoneNoLogin(userInfo).then(response => {
          if(response.code =='200'){
            const result = response.result
            const userInfo = result.userInfo
            Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_NAME, userInfo.username, 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
            commit('SET_TOKEN', result.token)
            commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname})
            commit('SET_AVATAR', userInfo.avatar)
            resolve(response)
          }else{
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    oaPhoneLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        oaPhoneLogin(userInfo).then(response => {
          console.log("！！！！！！！！！！！！！！！！！！response",response)
          if(response.code =='200'){
            const result = response.result
            const userInfo = result.userInfo
            console.log(userInfo)
            Vue.ls.set(ACCESS_TOKEN, result.token, 30 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_NAME, userInfo.realname, 30 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_INFO, userInfo, 30 * 24 * 60 * 60 * 1000)
            commit('SET_TOKEN', result.token)
            commit('SET_AVATAR', userInfo.avatar)
            commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname})
            resolve(response)
          }else{
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    Logout({ commit, state }) {
      return new Promise((resolve) => {
        let logoutToken = state.token;
        commit('SET_TOKEN', '')
        Vue.ls.remove(ACCESS_TOKEN)
        logout(logoutToken).then(() => {
          resolve()
        }).catch(() => {
          resolve()
        })
      })
    },

  },
  getters:{
    token: state => state.token,
    pageActive:state=>state.mainPageActive,
    username: state => {state.username = Vue.ls.get(USER_NAME);return state.username},
    nickname: state => {state.realname = Vue.ls.get(USER_INFO).realname; return state.realname},
    avatar: state => {state.avatar = Vue.ls.get(USER_INFO).avatar; return state.avatar},
    userid:state => {state.userid = Vue.ls.get(USER_INFO).id; return state.userid},
    phone:state => {state.phone = Vue.ls.get(USER_INFO).phone; return state.phone},
    mqClient:state => state.mqClient,
  }
})
